|
closesocket closes the socket indicated by the socket descriptor and releases all the resources for the socket.
Syntax
int closesocket(
__in SOCKET s
);
Parameters
s
Socket descriptor for a socket created by socket.
Return Values
INVALID_SOCKET if the function fails
Use WSAGetLastError to retrieve a specific error code.
Remarks
If an application calls closesocket and the linger option is enabled on the blocking socket and the linger timeout is set to non-zero, then the RT-TCP/IP stack will block the caller until the TCP state of the connection changes to CLOSED or TIME_WAIT internally or until the linger timeout expires, whichever comes first. TCP will continue to attempt to deliver any data left in the socket send queue until it is all acknowledged or until the linger timeout expires. On a non blocking socket, the caller shall not be blocked regardless of the state of the linger option and closesocket will return immediately. Linger has no affect on a non-blocking socket unless the linger timeout is zero.
If linger is enabled but the linger timeout is set to zero, when closesocket is called, the socket will be freed immediately and the TCP connection will be reset. The call to closesocket returns with SOCKET_ERROR with the error set to WSAECONNABORTED.
If the linger option is not enabled on the socket, which is the default, then TCP will attempt to close the connection gracefully. If there is data remaining in the socket send queue, it will attempt to deliver it to the target.
An event handle is held for a time period equal to the Stack timer tick when a TCP socket is closed. To avoid running out of events during periods where TCP sockets are closed and opened in quick succession, ensure that the Stack is initialized with a NumStartupEvents value greater than the number of sockets.